﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using CoastalClimbing.Code;
using System.Data;

namespace CoastalClimbing
{
    public partial class SiteMasterPage : System.Web.UI.MasterPage
    {
        public HtmlGenericControl BodyTag
        {
            get
            {
                return MasterPageBodyTag;
            }
            set
            {
                MasterPageBodyTag = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                string title = Page.Title;
                string keywords = string.Empty;
                string description = string.Empty;

                DataTable meta = DAL.MetaGet(title);

                var results = from row in meta.AsEnumerable() 
                                select new
                                {
                                    Description = row.Field<string>("DESCRIPTION"), 
                                    MetaType = row.Field<string>("METATYPE")
                                };

                foreach (var item in results)
                {
                    switch (item.MetaType)
	                {
                        case "title":
                        Page.Title = (string.IsNullOrWhiteSpace(item.Description) ? title : item.Description);
                        break;

                        case "keyword":
                        keywords += (item.Description.ToLower() + ",");
                        break;

                        default:
                        description = item.Description;
                        break;
	                }                              
                }
                
                AddMetaTag(litDesc, "description", description);
                AddMetaTag(litKey, "keywords", keywords);
                AddMetaTag(litCopy, "copyright", string.Format("© {0} Coastal Climbing. All rights reserved.", DateTime.Now.Year.ToString()));
            }
        }

        private void AddMetaTag(Literal literal, string name, string content)
        {
            string text = "<meta name='{0}' content='{1}' />";
            literal.Text = string.Format(text, name, content);
        }
    }
}